package com.zmm.myyimixian.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.db.ShoppingCart;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/2 0002.
 */
public class ShoppingCartAdapter extends BaseAdapter {

    private List<ShoppingCart> mList;
    private ModifyCountInterface modifyCountInterface;
    private int count = 0;//默认数量

    //接口回调
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    public void setData(List<ShoppingCart> list) {
        this.mList = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //绑定数据
        ShoppingCart shoppingCart = mList.get(position);

        holder.shoppingTitleItem.setText(shoppingCart.getTitle());
        holder.shoppingDescItem.setText(shoppingCart.getDesc());
        holder.shoppingPriceItem.setText("¥" + shoppingCart.getPrice());
        holder.shoppingCountItem.setText(shoppingCart.getCount());
        //加载图片
        Glide.with(parent.getContext())
                .load(ConfigPath.getLunBoPath(shoppingCart.getImgUrl()))
                .placeholder(R.mipmap.home_item_default_image)
                .into(holder.shoppingImgItem);
        Glide.with(parent.getContext())
                .load(ConfigPath.getLunBoPath(shoppingCart.getNewPromoType()))
                .into(holder.shoppingSmallImgItem);
        holder.shoppingItemJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //商品数量增加
                modifyCountInterface.doIncrease(position, holder.shoppingCountItem, holder.shoppingItemJian, holder.shoppingItemJia, count);
            }
        });
        holder.shoppingItemJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //商品数量减少
                modifyCountInterface.doDecrease(position, holder.shoppingCountItem, holder.shoppingItemJian, holder.shoppingItemJia, count);
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.shopping_img_item)
        ImageView shoppingImgItem;
        @BindView(R.id.shopping_small_img_item)
        ImageView shoppingSmallImgItem;
        @BindView(R.id.shopping_title_item)
        TextView shoppingTitleItem;
        @BindView(R.id.shopping_desc_item)
        TextView shoppingDescItem;
        @BindView(R.id.shopping_price_item)
        TextView shoppingPriceItem;
        @BindView(R.id.shopping_count_item)
        TextView shoppingCountItem;
        @BindView(R.id.shopping_item_jian)
        ImageButton shoppingItemJian;
        @BindView(R.id.shopping_item_jia)
        ImageButton shoppingItemJia;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
